# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2018/11/3 11:11
desc: 智讯产品的通用函数库
'''
import logging,random
from util.db_util import execute_query
import  base.globalvars as glo

market_code_dict = {'1': ['1001', '1002', '1003', '1012'], '2': ['1006', '1007'], '3': ['1017', '1021', '1022', '1023', '1024'], '4': ['1005', '1008']}  #使用SH,SZ无法区分B股


def get_A_stock_code_list():
    '''获取A股股票代码列表，设置全局变量'''
    sql = "SELECT code,abbr FROM dict_company WHERE mkt = 'A'"
    resultset = execute_query(sql)

    glo.set_value('A_Stock_List',resultset)
    return resultset #tuple: (('002054_SZ_EQ', '德美化工'), ('002055_SZ_EQ', '得润电子'), ('002056_SZ_EQ', '横店东磁'),...)


def get_random_A_stock_code():
    resultset = glo.get_value('A_Stock_List')
    if resultset is None:
        resultset = get_A_stock_code_list()

    rand_idx = random.randint(0,len(resultset)-1)

    logging.debug("random code:%s" % resultset[rand_idx][0])
    return resultset[rand_idx][0]


def get_random_A_stock_abbr():
    """获取随机的A股股票名称，在Excel testcase中被调用"""
    resultset = glo.get_value('A_Stock_List')
    if resultset is None:
        resultset = get_A_stock_code_list()
    rand_idx = random.randint(0,len(resultset)-1)
    logging.debug("rand_idx:%s"%rand_idx)
    logging.debug("random abb_szhr:%s" % resultset[rand_idx][1])
    return resultset[rand_idx][1]

def get_eng_label(csfid):
    pass


def get_data_by_criteria(tablename, criteria_str, return_fields):
    resultset = execute_query("SELECT %s FROM %s WHERE %s"%(return_fields, tablename, criteria_str))
    if resultset is None:
        return ((),)
    else:
        return resultset